<template>
	<view>
		<!-- 收货地址 -->
		<view class="addr" @tap="selectAddress">
			<!-- 		<view class="icon">
				<image src="../../static/img/addricon.png" mode=""></image>
			</view> -->
			<view class="right">
				<view class="tel-name">
					<view class="name">{{ recinfo.consigneeName }}</view>
					<view class="tel">{{ recinfo.phone }}</view>
				</view>
				<view class="addres">{{ recinfo.address}}</view>
			</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" v-for="(row, index) in buylist" :key="index">
				<view class="goods-info">
					<view class="img">
						<image :src="row.itemImage"></image>
					</view>
					<view class="info">
						<view class="title">{{ row.itemName }}</view>
						<view class="spec">{{i18n.choice}}{{ row.specificationNames }}</view>
						<view class="price-number">
							<view class="price">￥{{ row.price * row.count }}</view>
							<view class="number">x{{ row.count }}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="remark">
				<view class="row">
					<view class="left">{{i18n.invoiceType}}</view>
					<view class="right marginal">
						{{i18n.doNotInvoice}}
						<uni-icons type="arrowright" size="14" color="#999"></uni-icons>
					</view>
				</view>
				<view class="row">
					<view class="left">{{i18n.buyerMessage}}</view>
					<view class="right"><input :placeholder="i18n.optionalRemarks" v-model="note" /></view>
				</view>
			</view>
		</view>
		<!-- 提示-备注 -->
		<view class="order">
			<view class="row">
				<view class="left">{{i18n.coupon}}</view>
				<view class="right ">
					{{i18n.notAvailable}}
					<uni-icons type="arrowright" size="14" color="#999"></uni-icons>
				</view>
			</view>
			<view class="row">
				<view class="left">{{i18n.deliveryMethod}}</view>
				<view class="right">{{i18n.expressDelivery}}</view>
			</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">{{i18n.commodityAmount}}</view>
				<view class="content">￥{{ goodsPrice | toFixed }}</view>
			</view>
			<view class="row">
				<view class="nominal">{{i18n.freight}}</view>
				<view class="content">￥{{ freight | toFixed }}</view>
			</view>
		</view>
		<view class="blck"></view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">
					{{i18n.total}}:
					<view class="money">￥{{ sumPrice | toFixed }}</view>
				</view>
				<view class="btn" @tap="toPay">{{i18n.toPay}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		computed: {
			i18n() {
				// 倒入你需要的对象
				return this.$t('confirmation')
			}
		},
		data() {
			return {
				buylist: [], //订单列表
				addressList: [], //订单列表
				goodsPrice: 0.0, //商品合计价格
				sumPrice: 0.0, //用户付款价格
				freight: 0.0, //运费
				note: '', //备注
				token: '',
				userId: '',
				microShopId: '',
				recinfo: {}
			};
		},
		onLoad() {
			const microShopId = uni.getStorageSync('microShopId');
			if (microShopId) {
				this.microShopId = microShopId;
				console.log(microShopId);
			}
			const token = uni.getStorageSync('token');
			if (token) {
				this.token = token;
				console.log(token);
			}
			const userId = uni.getStorageSync('userId');
			if (userId) {
				this.userId = userId;
				console.log(userId);
			}
			this.getLanguage();
			this.getCartList(token);
			this.getAddressList(userId, token);
		},
		onShow() {
			//页面显示时，加载订单信息
			uni.getStorage({
				key: 'buylist',
				success: ret => {
					console.log(ret)
					this.buylist = ret.data;
					this.goodsPrice = 0;
					//合计
					let len = this.buylist.length;
					for (let i = 0; i < len; i++) {
						this.goodsPrice = this.goodsPrice + this.buylist[i].count * this.buylist[i].price;
					}
					// this.deduction = this.int / 100;
					this.sumPrice = this.goodsPrice + this.freight;
				}
			});
			uni.getStorage({
				key: 'selectAddress',
				success: e => {
					this.recinfo = e.data;
					uni.removeStorage({
						key: 'selectAddress'
					});
				}
			});
		},
		onHide() {},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder();
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			getAddressList(userId, token) {
				uni.request({
					url: 'http://121.40.82.3:52019/v1/template001/consignee/list', //获取购物车列表
					data: {
						pageNum: 1,
						pageSize: 10,
						ecCustomerId: userId
					},
					method: 'GET',
					dataType: 'json',
					header: {
						'token': token,
						'content-type': 'application/json' //自定义请求头信息
					},
					success: (res) => {
						console.log(res.data.data);
						this.addressList = res.data.data.list;
						this.addressList.map(res => {
							console.log(res)
							if (res.isDefault == '1') {
								this.recinfo = res
							}
						})

					}
				});
			},
			getCartList(token) {
				uni.request({
					url: 'http://121.40.82.3:52019/v1/template001/shoppingCart/list', //获取购物车列表
					data: {
						// microShopId: "c0a8007c-6e48-18b9-816e-48d048c70043"
						microShopId:this.microShopId
					},
					method: 'GET',
					dataType: 'json',
					header: {
						'token': token,
						'content-type': 'application/json' //自定义请求头信息
					},
					success: (res) => {
						console.log(res.data.data);
						this.sumPrice = res.data.data.shoppingCart.totalPrice;
						this.freight = res.data.data.shoppingCart.freight;
						this.shoppingCartItems = res.data.data.shoppingCart.shoppingCartItemGroups[0].shoppingCartItems;
						this.buylist = res.data.data.shoppingCart.shoppingCartItemGroups[0].shoppingCartItems;
						console.log(this.shoppingCartItems)

						this.goodsPrice = 0;
						//合计
						let len = this.shoppingCartItems.length;
						for (let i = 0; i < len; i++) {
							this.goodsPrice = this.goodsPrice + this.shoppingCartItems[i].count * this.shoppingCartItems[i].price;
						}
						this.sumPrice = this.goodsPrice + this.freight;
					}
				});
			},
			createOrder() {
				var date = this.getDate();
				console.log(date);
				uni.request({
					url: 'http://121.40.82.3:52019/v1/template001/ecOrder/generate', //获取购物车列表
					data: {
						microShopId: this.microShopId,
						// microShopId: "c0a8007c-6e48-18b9-816e-48d048c70043",
						consigneeId: this.recinfo.id, //收货地址ID
						couponId: " ", //优惠券ID
						payTypeCodes: " ", //支付方式(多种支付方式用','隔开，优先货到付款、再在线支付)
						currencyTypeId: " ", //交易币种ID
						orderType: " ", //订单类型
						needInvoice: 1, //是否需要发票 需要： 1 不需要：0
						invoiceType: "general", //发票类型 普通发票：general  专用发票：special
						invoiceBelong: "personal", //发票隶属 个人：personal 公司：company
						invoiceTitle: "测试抬头", //发票抬头
						invoiceContent: "测试内容", //发票内容
						createDate: date, //
						memo: this.note
					},
					method: 'POST',
					dataType: 'json',
					header: {
						'token': this.token,
						'content-type': 'application/x-www-form-urlencoded ' //自定义请求头信息
					},
					success: (res) => {
						console.log(res);
						alert(res.data.data.message)
						uni.switchTab({
							url: '/pages/tabBar/user/user'
						});
					}
				});
			},
			getLanguage() {
				var that = this;
				console.log(that.$i18n.locale)
				if (that.$i18n.locale === 'zh-CN') {
					uni.setNavigationBarTitle({
						title: '确认订单'
					});
				} else if (that.$i18n.locale === 'en-US') {
					uni.setNavigationBarTitle({
						title: 'Confirm order'
					});
				}
			},
			clearOrder() {
				uni.removeStorage({
					key: 'buylist',
					success: res => {
						this.buylist = [];
						console.log('remove buylist success');
					}
				});
			},
			toPay() {
				this.createOrder();
				// //商品列表
				// let paymentOrder = [];
				// let goodsid = [];
				// let len = this.buylist.length;
				// for (let i = 0; i < len; i++) {
				// 	paymentOrder.push(this.buylist[i]);
				// 	goodsid.push(this.buylist[i].id);
				// }
				// if (paymentOrder.length == 0) {
				// 	uni.showToast({
				// 		title: '订单信息有误，请重新购买',
				// 		icon: 'none'
				// 	});
				// 	return;
				// }
				//本地模拟订单提交UI效果
				// uni.showLoading({
				// 	title: '正在提交订单...'
				// });
				// setTimeout(() => {
				// 	uni.setStorage({
				// 		key: 'paymentOrder',
				// 		data: paymentOrder,
				// 		success: () => {
				// 			uni.hideLoading();
				// 			uni.redirectTo({
				// 				url: '../pay/payment/payment?amount=' + this.sumPrice
				// 			});
				// 		}
				// 	});
				// }, 1000);

				//模板模拟支付，实际应用请调起微信/支付宝
				// uni.showLoading({
				// 	title: '支付中...'
				// });
				// setTimeout(() => {
				// 	uni.hideLoading();
				// 	uni.showToast({
				// 		title: '支付成功'
				// 	});
				// 	setTimeout(() => {
				// 		uni.redirectTo({
				// 			url: '/pages/pay/success/success?amount=' + this.sumPrice
				// 		});
				// 	}, 300);
				// }, 700);
			},
			//选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '../user/address/address?type=select'
				});
			},
			getDate() {
				var date = new Date();
				var seperator1 = "-";
				var seperator2 = ":";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				var hours = date.getHours();
				var minutes = date.getMinutes();
				var seconds = date.getSeconds();
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				if (hours >= 0 && hours <= 9) {
					hours = "0" + hours;
				}
				if (minutes >= 0 && minutes <= 9) {
					minutes = "0" + minutes;
				}
				if (seconds >= 0 && seconds <= 9) {
					seconds = "0" + seconds;
				}
				var currentdate = year + seperator1 + month + seperator1 + strDate + " " + hours + seperator2 + minutes +
					seperator2 + seconds;
				return currentdate;
			}
		}
	};
</script>

<style lang="scss">
	.addr {
		width: 86%;
		padding: 20upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		display: flex;
		line-height: 1.5;

		.icon {
			width: 80upx;
			height: 80upx;
			display: flex;
			align-items: center;

			image {
				width: 60upx;
				height: 60upx;
			}
		}

		.tel-name {
			width: 100%;
			display: flex;
			align-items: center;
			font-size: 32upx;

			.tel {
				margin-left: 40upx;
			}
		}

		.addres {
			width: 100%;
			font-size: 26upx;
			color: #999;
		}
	}

	.buy-list {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 22vw;
					height: 22vw;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 22vw;
						height: 22vw;
					}
				}

				.info {
					width: 100%;
					height: 22vw;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 22upx;
						background-color: #f3f3f3;
						color: #a7a7a7;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 0 10upx;
						border-radius: 20upx;
						margin-bottom: 20vw;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;

						.price {
							color: #f52c38;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;
						}
					}
				}
			}
		}
	}

	.remark {
		margin: 30upx auto 20upx auto;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;

			.left {
				font-size: 28upx;
				flex-shrink: 0;
			}

			.right {
				margin-left: 40upx;
				font-size: 26upx;
				color: #999;
				display: flex;
				align-items: center;
				flex-grow: 1;

				&.marginal {
					margin-left: auto;
					color: #000;
					flex-grow: 0;
				}

				input {
					font-size: 26upx;
					color: #999;
					width: 100%;
				}
			}
		}
	}

	.order {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;
			align-items: center;

			.left {
				font-size: 28upx;
			}

			.right {
				margin-left: auto;
				font-size: 26upx;
				color: #000;
				display: flex;
				align-items: center;
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}

	.footer {
		width: 92%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;

		.settlement {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.sum {
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;
				align-items: baseline;

				.money {
					font-weight: 600;
					font-size: 36upx;
					color: #d10409;
				}
			}

			.btn {
				padding: 0 30upx;
				height: 60upx;
				background-color: #f5291c;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.nominal {
				font-size: 28upx;
			}

			.content {
				font-size: 26upx;
				color: #f06c7a;
			}
		}
	}
</style>
